<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class AdminEquipSysConfig extends Base
{
    protected $table = 'admin_equip_auction_config';
    protected $equipAuctionChatConfig = 'equip_auction_chat_config';
    protected $equipAuctionChatReport = 'equip_auction_chat_report';

    protected $equipAuctionChatConfigRedis = 'EQUIP:AUCTION:CHAT:CONFIG';//后台装备公聊大厅配置缓存 string EQUIP:AUCTION:CHAT:CONFIG=>json

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //更新系统回收装备状态
    public function equipSyschange($id,$status){
        $res = DB::table($this->table)->where('id',$id)->update(['status'=>$status,'updated_at'=>date('Y-m-d H:i:s',time())]);
        return $res;
    }
    
    //新增回收用户
    public function AddequipSysconfig($userid,$minutes){
        $data = [
            'user_id' => $userid,
            'minutes' => $minutes,
            'status' => 1,
            'created_at' => date('Y-m-d H:i:s',time())
        ];
        $res = DB::table($this->table)->insert($data);
        return $res;
    }

    //公聊配置修改
    public function insertOrUpdate($data, $id){
        unset($data['id']);
        if(empty($id)){
            $res = DB::table($this->equipAuctionChatConfig)->insert($data);
        }else{
            $res = DB::table($this->equipAuctionChatConfig)->where('id',$id)->update($data);
            Redis::DEL($this->equipAuctionChatConfigRedis);
        }
        return $res;
    }

    //获取公聊配置
    public function getConfig(){
        $redisdata = Redis::get($this->equipAuctionChatConfigRedis);
        if(empty($redisdata)){
            $data = DB::table($this->equipAuctionChatConfig)->where('status',1)->first();
            if(empty($data)){
                return $data;
            }
            $data = self::objToArray($data);
            Redis::SET($this->equipAuctionChatConfigRedis, json_encode($data));
            Redis::EXPIRE($this->equipAuctionChatConfigRedis,7*86400);
        }else{
            $data = json_decode($redisdata,true);
        }
        return $data;
    }
    
    //举报处理
    public function updateReport($id,$params){
        $status = DB::table($this->equipAuctionChatReport)->where('id',$id)->update($params);
        return $status;
    }

    //举报详细
    public function getInfo($id){
        $data = DB::table($this->equipAuctionChatReport)->where('id',$id)->first();
        $data = objToArray($data);
        return $data;
    }
}